package com.zdx.yswl.ui.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.R;
import com.zdx.yswl.R.color;
import com.zdx.yswl.http.KAsyncTask.TaskDelegate;
import com.zdx.yswl.ui.adapter.PicturesViewPagerAdapter;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.bean.PictruesBean;
import com.zdx.yswl.ui.exception.AppException;
import com.zdx.yswl.ui.exception.NoDataException;
import com.zdx.yswl.ui.task.AllPictureTask;

/**
 * 效果图界面
 * 
 * @author kangpeng
 *
 */
public class AllPictureActivity extends BaseActivity implements OnClickListener, TaskDelegate, OnPageChangeListener {

	protected static final String TAG = AllPictureActivity.class.getSimpleName();
	private Button mxgt;
	private Button mybj;
	private Button msjt;
	private Button mzb;
	TextView picturetype_count;
	private ViewPager mViewpager;
	PicturesViewPagerAdapter mAdapter;
	private PictruesBean bean;
	private String requestId;
	int total_lenght;

	public static Intent actionStart(Context context, String requestId) {
		Intent intent = new Intent(context, AllPictureActivity.class);
		intent.putExtra("projectId", requestId);
		return intent;
	}

	@Override
	protected void initSettings()
	{

	}

	@Override
	protected void initUi()
	{

	}

	@Override
	protected void saveSettings()
	{

	}

	@Override
	protected void freeMe()
	{

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_allpicture);

		requestId = getIntent().getStringExtra("projectId");
		setHeader("1/1");
		initView();
	}

	private void initView() {
		picturetype_count = (TextView) findViewById(R.id.picturetype_count);
		picturetype_count.setText("效果图  1/1");
		mxgt = (Button) findViewById(R.id.xgt_btn);// 效果图
		mybj = (Button) findViewById(R.id.ybj_btn);// 样板间
		msjt = (Button) findViewById(R.id.sjt_btn);// 实景图
		mzb = (Button) findViewById(R.id.zb_btn); // 周边配套
		mxgt.setOnClickListener(this);
		mybj.setOnClickListener(this);
		msjt.setOnClickListener(this);
		mzb.setOnClickListener(this);
		mViewpager = (ViewPager) findViewById(R.id.photoview_pager);
		mViewpager.setOnPageChangeListener(this);
		mAdapter = new PicturesViewPagerAdapter(this);
		mViewpager.setAdapter(mAdapter);
		
		/**请求*/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectId", requestId);
		AllPictureTask task = new AllPictureTask(-1, this);
		task.execute(map);
	}

	@Override
	public void onClick(View v) {
		if (null == bean)
			return;
		switch (v.getId()) {
		case R.id.xgt_btn:
			setEffectBtnBg();
			mViewpager.setCurrentItem(0);
			break;
		case R.id.ybj_btn:
			setSampleBtnBg();
			mViewpager.setCurrentItem(bean.effectPicList.size());
			break;
		case R.id.sjt_btn:
			setRealBtnBg();
			mViewpager.setCurrentItem(bean.effectPicList.size() + bean.samplePicList.size());
			break;
		case R.id.zb_btn:
			setAroundBtnBg();
			mViewpager.setCurrentItem(bean.effectPicList.size() + bean.samplePicList.size() +bean.realPicList.size());
			break;

		default:
			break;
		}
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {

		if (object instanceof NoDataException) {
			Toast.makeText(this, ((NoDataException) object).getMessage(), Toast.LENGTH_SHORT).show();
		} else if (object instanceof AppException) {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
//			ConfirmDialog.share(this, -1, new ConfirmDialogDelegate() {
//				@Override
//				public void onClick_ConfirmDialog(int __id, int __position) {
//					finish();
//				}
//			});
//			ConfirmDialog.showDialog("提示", ((AppException) object).getMessage(), "确定");
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {

		bean = (PictruesBean) object;
		total_lenght = bean.aroundPicList.size() + bean.effectPicList.size() + //
				 bean.realPicList.size() + bean.samplePicList.size();
		setHeader(1 + "/" + total_lenght);
		mAdapter.setData(bean);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onPageScrollStateChanged(int postion) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int postion) {
		postion = postion + 1 ; 
		if (total_lenght > 0) {
			super.mTitle.setText(postion + "/" + total_lenght);
		}
		
		int effTemp = bean.effectPicList.size();
		if (postion > 0 && postion <= effTemp) {
			picturetype_count.setText("效果图  "+ postion+ "/" +effTemp);
			setEffectBtnBg();
		}
		int sampTemp = bean.samplePicList.size();
		if (postion > effTemp && postion <= (sampTemp+effTemp)) {
			picturetype_count.setText("样板间  "+(postion - effTemp)+ "/" +sampTemp);
			setSampleBtnBg();
		}
		int realTemp = bean.realPicList.size();
		if (postion > (sampTemp+effTemp) && postion <= (sampTemp+effTemp+realTemp)) {
			picturetype_count.setText("实景图  "+(postion - (sampTemp+effTemp))+ "/" +realTemp);
			setRealBtnBg();
		}
		int aroundTemp = bean.aroundPicList.size();
		if (postion > (sampTemp+effTemp+realTemp) && postion <= total_lenght) {
			picturetype_count.setText("周边配套 "+(postion - (sampTemp+effTemp+realTemp))+ "/" +aroundTemp);
			setAroundBtnBg();
		}
	}

	
	private void setAroundBtnBg(){
		mxgt.setBackgroundResource(R.drawable.photobutton_selector_two);
		mxgt.setTextColor(color.photo_btn_title);
		mybj.setBackgroundResource(R.drawable.photobutton_selector_two);
		mybj.setTextColor(color.photo_btn_title);
		msjt.setBackgroundResource(R.drawable.photobutton_selector_two);
		msjt.setTextColor(color.photo_btn_title);
		mzb.setBackgroundResource(R.drawable.photobutton_selector);
		mzb.setTextColor(getResources().getColor(R.color.main_while));
	}
	private void setRealBtnBg(){
		mxgt.setBackgroundResource(R.drawable.photobutton_selector_two);
		mxgt.setTextColor(color.photo_btn_title);
		mybj.setBackgroundResource(R.drawable.photobutton_selector_two);
		mybj.setTextColor(color.photo_btn_title);
		msjt.setBackgroundResource(R.drawable.photobutton_selector);
		msjt.setTextColor(getResources().getColor(R.color.main_while));
		mzb.setBackgroundResource(R.drawable.photobutton_selector_two);
		mzb.setTextColor(color.photo_btn_title);
	}
	private void setEffectBtnBg(){
		mxgt.setBackgroundResource(R.drawable.photobutton_selector);
		mxgt.setTextColor(getResources().getColor(R.color.main_while));
		mybj.setBackgroundResource(R.drawable.photobutton_selector_two);
		mybj.setTextColor(color.photo_btn_title);
		msjt.setBackgroundResource(R.drawable.photobutton_selector_two);
		msjt.setTextColor(color.photo_btn_title);
		mzb.setBackgroundResource(R.drawable.photobutton_selector_two);
		mzb.setTextColor(color.photo_btn_title);

	}
	private void setSampleBtnBg(){
		mxgt.setBackgroundResource(R.drawable.photobutton_selector_two);
		mxgt.setTextColor(color.photo_btn_title);
		mybj.setBackgroundResource(R.drawable.photobutton_selector);
		mybj.setTextColor(getResources().getColor(R.color.main_while));
		msjt.setBackgroundResource(R.drawable.photobutton_selector_two);
		msjt.setTextColor(color.photo_btn_title);
		mzb.setBackgroundResource(R.drawable.photobutton_selector_two);
		mzb.setTextColor(color.photo_btn_title);
	}
	
	/**友盟session的统计*/
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/**友盟session的统计*/
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj)
	{

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj)
	{

	}
}
